package com.example.review.Fragment;


import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.example.review.ChildFragment.ImgFragment;
import com.example.review.R;
import com.google.android.material.tabs.TabLayout;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * A simple {@link Fragment} subclass.
 */
public class VarPageFragment extends Fragment {
    @BindView(R.id.viewpager)
    ViewPager viewpager;
    @BindView(R.id.tab)
    TabLayout tab;
    private View view;
    private Timer timer;
    private TimerTask timerTask;
    private int i=0;

    public VarPageFragment() {
        // Required empty public constructor
    }
    //回调方法
    @Override
    public void onDestroy() {
        super.onDestroy();
        //取消定时器
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_var_page, container, false);
        ButterKnife.bind(this,view);
        List<Fragment> list = new ArrayList<>();
        list.add(new ImgFragment(0));
        list.add(new ImgFragment(1));
        list.add(new ImgFragment(2));
        list.add(new ImgFragment(3));
        viewpager.setAdapter(new myAdapter(getChildFragmentManager(), list));
        tab.setupWithViewPager(viewpager);
        timer=new Timer();
        timerTask=new TimerTask() {
            @Override
            public void run() {
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        viewpager.setCurrentItem(i);
                        if(i==list.size()-1){
                            i=0;
                        }else {
                            i++;
                        }
                    }
                });

            }
        };
        //设置定时时间
        timer.schedule(timerTask,0,3000);
        return view;
    }
    /*
    * @param fm
    *         碎片管理器
    * @param list
    *         Fragment集合
    * */
    private class myAdapter extends FragmentPagerAdapter {
        List<Fragment> list;

        public myAdapter(FragmentManager fm, List<Fragment> list) {
            super(fm);
            this.list = list;
        }

        @Override
        public Fragment getItem(int position) {
            return list.get(position);
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return "";
        }
    }
}
